package com.sgb.kjwl.viewmodel.transport;

import android.app.Application;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.ProListEntity;
import com.sgb.kjwl.model.entity.transport.GoodsInfoEntity;
import com.sgb.kjwl.model.entity.transport.SearchGoodsEntity;
import com.sgb.kjwl.utils.LocationUtil;
import com.sgb.kjwl.view.ui.fragment.transport.SearchTransportGoodsFragment;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author: 王彬臣
 * @date: 2021/2/18
 * @Description:
 */
public class SearchGoodsModel extends AndroidViewModel {
    private int pageSize = 10;
    private int pageNum = 1;

    public SearchGoodsModel(@NonNull Application application) {
        super(application);
    }

    public void searchCatogories(SearchTransportGoodsFragment fragment, String searchType, String keyWord, boolean isLoadMore) {
        if (TextUtils.isEmpty(searchType) || TextUtils.isEmpty(keyWord)) {
            return;
        }
        if (isLoadMore) {
            pageNum++;
        } else {
            pageNum = 1;
        }
        Map map = new HashMap();
        map.put("page", pageNum);
        map.put("limit", pageSize);
        map.put("keyWord", keyWord);
        if(LocationUtil.isOpenLocation(fragment.getActivity())) {
            map.put("longitude", fragment.currentLatLng.longitude);
            map.put("latitude", fragment.currentLatLng.latitude);
        }
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getLogisticsAPI().getGoodsBySearch(map).enqueue(new Callback<BaseEntity<ProListEntity<List<SearchGoodsEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ProListEntity<List<SearchGoodsEntity>>>> call, Response<BaseEntity<ProListEntity<List<SearchGoodsEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.refreshGoodsInfoList(response.body().getData().getList(), response.body().getData().getTotalRow(),isLoadMore);
                } else {
                    MToast.showToast(fragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ProListEntity<List<SearchGoodsEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
